<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 输入、处理、输出
        //  indexOf()  方法返回在数组中可以找到一个
        //  给定元素的第一个索引，如果不存在，则返回-1.

        let arr = ['ant', 'bison', 'camel', 'duck', 'bison', 'camel'];

        console.log(arr.indexOf("camel"));
        console.log(arr.indexOf("camel1"));


        // lastIndoxOf() 数组从后往前数，找到第一个符合条件的元素的索引号
        console.log(arr.lastIndexOf("camel"));

        function indexOf(arr, ele) {
            var index = -1;
            if (!(arr instanceof Array)) {
                return -1;
            }
            for (var i = 0; i < arr.length; i++) {
                if (ele == arr[i]) {
                    index = i;
                    break;
                }
                
            }
            return index;
        }
        console.log(indexOf(arr, 'abc'));
        console.log(indexOf(arr, 'camel'));


    </script>
</body>
</html>